package com.novunda.license.core;

/**
 * @author linfeng
 * @since 2016/10/12.
 */
public class LicenseManager implements LicenseStore, LicenseVerifier {

    private final LicenseEncode encode;
    private final LicenseDecode decode;
    private LicenseValidate validate;
    private LicenseContent licenseContent;

    public LicenseManager(String privateKey) {
        encode = new SM2Encoder(privateKey);
        decode = new SM2Decode(privateKey);
        validate = new BaseLicenseValidate();
    }

    public LicenseValidate getValidate() {
        return validate;
    }

    public void setValidate(LicenseValidate validate) {
        this.validate = validate;
    }

    public LicenseEncode getEncode() {
        return encode;
    }

    public LicenseDecode getDecode() {
        return decode;
    }

    public LicenseContent getLicenseContent() {
        return licenseContent;
    }

    public void setLicenseContent(LicenseContent licenseContent) {
        this.licenseContent = licenseContent;
    }

    @Override
    public synchronized String create(LicenseContent content) throws LicenseException {
        return encode.encode(content);
    }

    @Override
    public synchronized void load(String lic) throws LicenseException {
        licenseContent = decode.decode(lic);
    }

    @Override
    public synchronized void unLoad() {
        licenseContent = null;
    }

    @Override
    public synchronized void verify() throws LicenseException {

        if (licenseContent != null) {
            validate.validate(licenseContent);
        }

    }

}
